package org.xvergabe.forms.poc.events;

import java.io.IOException;

import org.xvergabe.xsd.forms.components.x20.String;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;

/**
 * einer {@link cell} zugefuegt bewirkt dieses Event die Darstellung eines
 * Eingabefeldes in dieser Zelle
 * 
 * @author schiller
 * 
 */
public class PdfFieldStringCellEvent implements PdfPCellEvent {

	String field;

	public PdfFieldStringCellEvent(String field) {
		this.field = field;
	}

	@Override
	public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {

		PdfWriter writer = canvases[0].getPdfWriter();
		TextField textfield = new TextField(writer, position, field.getId());

		try {
			textfield.setText(field.getValue());
			writer.addAnnotation(textfield.getTextField());
		} catch (DocumentException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
